<?php defined('SYSPATH') OR die('No direct access allowed.');

class Forum_User_Model extends User_Model {

	protected $has_one = array('userinfo' => 'forum_user_info', 'user_detail');
	protected $load_with = array('userinfo');
	protected $table_name = 'users';
	protected $has_many = array('posts', 'comments', 'moderated_categories');
	protected $has_and_belongs_to_many = array('roles');
	protected $foreign_key = array('posts'=>'author_id', 'comments'=>'author_id');

	public function save() {
		if ($this->userinfo->changed) $this->userinfo->save();
	}

	public function increase_count($column, $value = 1) {
		$column = $column."_count";
		if (array_key_exists($column, $this->userinfo->table_columns)) {
			$this->userinfo->$column += intval($value);
			$this->userinfo->save();
		}
	}

	public function get_role_id() {
		return $this->role;
	}

	public function get_moderated_categories() {
		return ORM::factory('moderated_category')->get_categories($this->primary_key_value);
		//return $this->db->select('category_id')->from($this->table_prefix().'category_moderators')->where('user_id', $this->primary_key_value)->get()->result(FALSE);
		//return $this->categories->primary_key_array();
	}

	public function is_moderator($category = NULL) {
		if (! $this->loaded) return FALSE;
		if (! $category instanceof Category_Model) {
			$category = ORM::factory('category', $category);
		}
		return $category->is_moderated_by($this->primary_key_value);
	}
}
